/******************************************************************************************
* Javascript created by Roberto Robson beicozitos@gmail.com on 09/05/2007
* The method 'x' does not exist in your class but you can import it at the first calling of the method, using this example
*
*  - Have a little problem to transfer the arguments from a function to another function
*  
******************************************************************************************/

iMethods=function(){
    var obj = this;
    var ext="js";
    var dir="ms";
    var methods=arguments[0];
    this.queueImportMethods=[];
    
    this.iMethodImport=function(){
      this.xhr = new ajaxGateway({"objName":"","url":dir+"/"+this.className+"."+arguments[0].jsFile+"."+ext,"async":false});
      eval("this.xhrCore="+this.xhr.xmlhttp.responseText);
      this[arguments[0].jsFile] = this.xhrCore;
      if(typeof this[arguments[0].jsFile] == "function")
        this[arguments[0].jsFile]( ((typeof arguments[0].args != "undefined")?arguments[0].args:"") );
      else
        this[arguments[0].jsFile];
    }
    
    this.importAjaxGateway=function(){
      if(typeof(this.iMethodsExt)=="undefined"){
        this.iMethodsExt=iMethods;
        this.iMethodsExt([
          ["Class","ajaxGateway",[],"/lib.uol.intranet/ajaxgateway/ajaxgateway.js",function(){ obj.runQueueMethods() }]
        ]);
        obj.ajaxGateway();
      }
    }
    
    this.runQueueMethods=function(){
      for(var i=0;i<this.queueImportMethods.length;i++){
        obj[this.queueImportMethods[i]['jsFile']](((typeof(this.queueImportMethods[i]['args'][0])!="undefined")?this.queueImportMethods[i]['args'][0]:""));
      }
    }
    
    this.addEachMethod=function(name){
      this[name]=function(){
        if(typeof(ajaxGateway)=="undefined"){
          this.importAjaxGateway();
          this.queueImportMethods[this.queueImportMethods.length]={"jsFile":name,"args":arguments};
        }else{
          this.iMethodImport({"jsFile":name,"args":arguments});
        }
        return this;
      }
    }

    var importEachMethodReplace=function(methodName,args,methodProp,methodExec,methodArgs){
      obj[methodName]=eval(methodName);
      if(typeof(obj[methodName])=="function")
        obj[methodName](args);
      else if(typeof(methodExec)!="undefined")
        eval(obj.className+"."+methodName+"."+methodExec+"(methodArgs);");
    }
    
    this.importEachMethod=function(methodProp){
      var methodProp=methodProp;
      var methodName=methodProp[1];
      var methodUrl=methodProp[3];
      var callBack=methodProp[4];

      this[methodName]=function(methodExec,methodArgs){
        iMethodsHtmlJs=document.createElement('script');
        var args=arguments[0];
        if(navigator.userAgent.indexOf("Safari")==-1 && navigator.userAgent.indexOf("Opera")==-1){
          try{
            iMethodsHtmlJs.addEventListener("load",function(){
              if(typeof(callBack)=="function")
                callBack();
              importEachMethodReplace(methodName,args,methodProp,methodExec,methodArgs);
              loaded=1;
            },true);
            
  
          }catch(e){
            try{
              iMethodsHtmlJs.onreadystatechange = function () {
                if (iMethodsHtmlJs.readyState == "loaded"|| iMethodsHtmlJs.readyState == "complete"){
                  if(typeof(callBack)=="function")
                    callBack();
                  importEachMethodReplace(methodName,args,methodProp,methodExec,methodArgs);
                }
              }
            }catch(e){}
  
          }
        }else{
          var iMethodsHtmlJsTimer;
          clearTimeout(iMethodsHtmlJsTimer);
          iMethodsHtmlJsTimer=setInterval(function(){
            if(eval("typeof("+methodName+")")!="undefined"){
              clearTimeout(iMethodsHtmlJsTimer);
              if(typeof(callBack)=="function")
                callBack();
              importEachMethodReplace(methodName,args,methodProp,methodExec,methodArgs);
            }
          },10);

        }
  
        iMethodsHtmlJs.src=methodUrl;
  	    document.getElementsByTagName('head')[0].appendChild(iMethodsHtmlJs);
        
        return this;
      }
      

      methodTpl=function(){
        var methodProp;
        this.setMethod=function(){
          methodProp=arguments[0];
        }
        this.mod=function(){
          return function(){ obj[methodProp['methodName']].init(methodProp['methodExec'],arguments[0])}
        }
      }
      
      if(methodProp[0]=="Object"){
        init=this[methodName];
        this[methodName]={};
        this[methodName].init=init;
        for(var i=0;i<methodProp[2].length;i++){
          var methodExec=methodProp[2][i];
          this[methodName][methodExec]=new methodTpl();
          this[methodName][methodExec].setMethod({methodName:methodName,methodExec:methodExec,args:arguments});
          this[methodName][methodExec]=this[methodName][methodExec].mod({});
        }
      }

      //this[methodName](methodName);

    }
    
    for(var i=0;i<methods.length;i++){
      if(methods[i].length>1 && methods[i][3].indexOf(".js")>=0)
        this.importEachMethod(methods[i]);
      else
        this.addEachMethod(methods[i]);
    }
}